package com.lsk.servicemap.remote;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.lsk.internalcommon.constant.AMapConfigConstants;
import com.lsk.internalcommon.response.DirectionResponse;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class MapDirectionClient {
    @Autowired
    private RestTemplate restTemplate;
    @Value("${map.key}")
    private String mapKey;

    public DirectionResponse direction(String deplongitude,String deplatitude,String destlongitude,String destlatitude){
        String url = spell(deplongitude, deplatitude, destlongitude, destlatitude);
        //调用请求
        ResponseEntity<String> direction = restTemplate.getForEntity(url, String.class);
        String body = direction.getBody();
        //解析返回参数
        DirectionResponse directionResponse = parseDirection(body);
        return directionResponse;
    }


    //拼接url
    private String spell(String deplongitude,String deplatitude,String destlongitude,String destlatitude){

        StringBuilder urlBuilder = new StringBuilder();
        urlBuilder.append(AMapConfigConstants.DIRECTION_URL);
        urlBuilder.append("?");
        urlBuilder.append("origin"+"="+deplongitude+","+deplatitude);
        urlBuilder.append("&");
        urlBuilder.append("destination"+"="+destlongitude+","+destlatitude);
        urlBuilder.append("&");
        urlBuilder.append("extensions=all"+"&"+"output=json");
        urlBuilder.append("&");
        urlBuilder.append("key="+mapKey);

        return urlBuilder.toString();
    }

    public DirectionResponse parseDirection(String body){
        DirectionResponse directionResponse = null;
        try {
            JSONObject result = JSONObject.fromObject(body);
            if(result.has(AMapConfigConstants.STATUS)){
                int status = result.getInt(AMapConfigConstants.STATUS);
                if (status==1){
                    if (result.has(AMapConfigConstants.ROUTE)){
                        JSONObject routeJson = result.getJSONObject(AMapConfigConstants.ROUTE);
                        JSONArray pathArray = routeJson.getJSONArray(AMapConfigConstants.PATHS);
                        JSONObject path = pathArray.getJSONObject(0);
                        directionResponse = new DirectionResponse();
                        if (path.has(AMapConfigConstants.DISTANCE)){
                            int distance = path.getInt(AMapConfigConstants.DISTANCE);
                            directionResponse.setDistance(distance);
                        }
                        if (path.has(AMapConfigConstants.DURATION)) {
                            int duration = path.getInt(AMapConfigConstants.DURATION);
                            directionResponse.setDuration(duration);
                        }
                    }
                }
            }
        }catch (Exception e){

        }
        return directionResponse;
    }
}
